package com.ym.b2b.provider.service.impl;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.ym.b2b.provider.dao.EmCProviderBelongBuyerDAO;
import com.ym.b2b.provider.service.EmCProviderBelongBuyerService;
import com.ym.b2b.provider.sqlbean.EmCProviderBelongBuyer;
import com.ym.b2b.user.dao.EmCUserDAO;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

/**
 * @ClassName: EmCProviderBelongBuyerServiceImpl
 * @Description: TODO
 * @Author tangxj
 * @Date 2015-3-19 上午11:19:09
 * @Modify
 * @CopyRight B2B小组
 */

@Service
public class EmCProviderBelongBuyerServiceImpl extends BaseService implements EmCProviderBelongBuyerService {

    private Logger                    logger = Logger.getLogger(getClass());
    @Autowired
    private EmCProviderBelongBuyerDAO emCProviderBelongBuyerDAO;

    @Autowired
    private EmCUserDAO                userdao;
    
    private Logger         dbLog = Logger.getLogger("db");

    /*
     * 分页查询
     */
    @Override
    public Result searchPageEmCProviderBelongBuyer(Page<EmCProviderBelongBuyer> page,
                                                   EmCProviderBelongBuyer emCProviderBelongBuyer)
                                                                                                 throws DataAccessException {
        Result result = new Result();

        try {
            result.setResultPage(emCProviderBelongBuyerDAO.searchPageEmCProviderBelongBuyer(emCProviderBelongBuyer,
                                                                                            page));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "查询异常");
        }
        return result;
    }

    /*
     * 插入
     */
    @Override
    public Result insertProviderBelongBuyer(EmCProviderBelongBuyer emCProviderBelongBuyer) throws DataAccessException {
        Result result = checkInputEmCProviderBelongBuyer(emCProviderBelongBuyer);
        if (!result.getCode().equals("200")) return result;
        try {
            if (!"".equals(emCProviderBelongBuyer.getBuyerCodeList())) {
                String[] buyerCodes = emCProviderBelongBuyer.getBuyerCodeList().split(",");
                for (String buyerCode : buyerCodes) {
                    emCProviderBelongBuyer.setBuyerCode(buyerCode);
                    emCProviderBelongBuyerDAO.insertEmCProviderBelongBuyer(emCProviderBelongBuyer);
                }
                dbLog.info("INSERT-->EM_C_PROVIDER_BELONG_BUYER 供货商("+emCProviderBelongBuyer.getProviderCode()+")添加客户 " +"  客户编码："+emCProviderBelongBuyer.getBuyerCodeList());
            }
            result.setResult("供应商设置客户成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "供应商设置客户失败！");

        }
        return result;
    }

    /*
     * 删除
     */
    @Override
    public Result deleteEmCProviderBelongBuyer(EmCProviderBelongBuyer emCProviderBelongBuyer)
                                                                                             throws DataAccessException {
        Result result = new Result();
        try {
            emCProviderBelongBuyerDAO.deleteEmCProviderBelongBuyer(emCProviderBelongBuyer);
            dbLog.info("DELETE-->EM_C_PROVIDER_BELONG_BUYER 供货商("+emCProviderBelongBuyer.getProviderCode()+")删除客户 " +"  数据值："+JSON.toJSONString(emCProviderBelongBuyer));
            result.setResult("删除供应客户成功！");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "删除供应商客户失败！");
        }

        return result;
    }

    @Override
    public Result deleteMultipleEmCProviderBelongBuyers(String belongIdList) {
        Result result = new Result();
        try {
            emCProviderBelongBuyerDAO.deleteMultipleEmCProviderBelongBuyers(belongIdList);
            dbLog.info("DELETE(Multiple-删除)-->EM_C_PROVIDER_BELONG_BUYER "+"ID: "+belongIdList);
            result.setResult("批量删除供应商客户成功！");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "批量删除供应商客户失败！");
        }

        return result;
    }

    /**
     * 输入参数检查
     * 
     * @Title: checkInputEmCProvider
     */
    private Result checkInputEmCProviderBelongBuyer(EmCProviderBelongBuyer p) {
        Result result = new Result();
        if (StringUtils.isBlank(p.getProviderCode())) {
            result.setCodeResult(Result.CODE_100, "缺少providerCode供应商编码!");
            return result;
        }
        if (StringUtils.isBlank(p.getBuyerCodeList())) {
            result.setCodeResult(Result.CODE_100, "缺少buyerCode采购商编码!");
            return result;
        }
        return result;

    }
}
